<? include 'server_config.php'; 

if($_SESSION['user_idx'] == "")
{
	?>
	<script>
		location.href='login.php';
	</script>
	<?
	return;
}
?>

			<div style='height:3px;width:100%;float:left;'><span><span></div>
			<div style='width:100%;background:#F2F2F2;float:left;border:1px solid #E9E9E9;padding-top:18px;'>
				<div style='width:25px;height:100px;float:left;'></div>
				<div style='width:326px;float:left;'>

						<?
//							$document_idx = 142;



							$sql="
								SELECT A.title, A.content, A.category_idx, A.user_idx, A.last_view_user_idx, B.nickname, A.reg_timestamp, 
									A.read_count
								FROM ndd_document A, ndd_user B WHERE A.document_idx = $document_idx AND A.user_idx = B.user_idx;
							";

							$result = mysql_query($sql,$connect);

							$row = mysql_fetch_row($result);

							if($row[3]=="")
							{
								?>
								<script>
								//location.href='board.php?name=<? echo $board_name;?>&category_idx=<? echo $category_idx; ?>&page=<? echo $page; ?>';
								</script>
								<?
								
								return;

							}

							$document_title = $row[0];
							$document_content = $row[1];
							$document_category_idx = $row[2];
							$document_user_idx = $row[3];
							$last_view_user_idx = $row[4];
							$documnet_user_nickname = $row[5];
							$document_timestamp = $row[6];
							$documnet_read_count = $row[7];

							if ($last_view_user_idx != $_SESSION['user_idx'])
							{
								$sql="
									UPDATE ndd_document SET read_count = read_count+1, last_view_user_idx = '".$_SESSION['user_idx']."' WHERE document_idx = $document_idx;
								";

								$result = mysql_query($sql,$connect);
							}



							$sql="
								SELECT name FROM ndd_category WHERE category_idx = $document_category_idx;
							";

							$result = mysql_query($sql,$connect);

							$row = mysql_fetch_row($result);

						?>





						<div style='width:73px;height:115px;float:left;'>
						<?
							$user_img = "./upload/member_photo/".$document_user_idx;

							if(file_exists($user_img)){
						?>
							<img src="<? echo $user_img; ?>" width="73px" height="73px" border=0>
						<?
							}
							else
							{
						?>
							<? if($document_user_idx == $_SESSION['user_idx'])
								{
							?>
								<a href="member_info.php">이미지등록</a>
							<?
								}
							?>
						<?
							}
						?>	
						</div>

						<div style='width:90px;height:30px;float:right;text-align:right;'>
							<a onclick="show_user_menu_reply('<?=$document_user_idx?>','<?=$documnet_user_nickname?>');" style="cursor:pointer;"><img src="./img/button_send_memo.png" border="0"></a><a onclick="add_friend('<?=$document_user_idx?>','<?=$documnet_user_nickname?>');" style="cursor:pointer;"><img src="./img/button_friend_add.png" border="0"></a>
						</div>

						<div style='width:15px;height:115px;float:left;'>
						</div>

						<div style='width:140px;height:115px;float:left;'>
							<span id='list_id'><? echo $documnet_user_nickname;?><br></span>
							<span id='list_id_detail'>



						<? 
						
							$sql_2="
								SELECT birthday, sex FROM ndd_user_expand WHERE user_idx = $document_user_idx LIMIT 1;
							";
									
							$result_2 = mysql_query($sql_2,$connect);
				
							$row2 = mysql_fetch_row($result_2);

							if($row2[1]=="M")
								echo "남";
							else if($row[1]=="F")
								echo "여";
							else 
								echo "(미입력)";
							
							echo " · ";

							if($row[0]!="0000-00-00" && $row2[0]!="")
							{

								//생일 입력
								$myBirthDate    = strtotime($row2[0]);

								//만나이
								$birthDate1     = date( 'Ymd', $myBirthDate );
								$nowDate1       = date('Ymd');
								$age1           = floor(($nowDate1 - $birthDate1) / 10000);

								//한국 나이
								$birthDate2     = date( 'Y', $myBirthDate );
								$nowDate2       = date('Y');
								$age2           = $nowDate2 - $birthDate2 + 1 ;

								echo $age2."세";
							}
							else
							{
								echo "(미입력)";

							}
				
						?>				
							<br>

							<?
							//류마티스 점수 읽어옴

							$sql3="
								SELECT score
								
								FROM ndd_ra_score_input
								WHERE user_idx = $document_user_idx

								ORDER BY reg_timestamp DESC LIMIT 1
								;
							";


							$result3 = mysql_query($sql3,$connect);
							$row3 = mysql_fetch_row($result3);
							$ra_score = $row3[0];

							?>


							류마점수 <?=$ra_score?>점 <span>
							<?

							for($i=1;$i<=$ra_score;$i++)
							{
								?><img src='./img/ra_score.png'><?
							}

							?>
							</span>
							<br>
							<!--회원등급 2급 ★★-->
							</span>

						</div>

						<div style='width:85px;height:85px;float:left;'>
						</div>

						<div style='clear:both;'><span></span></div>

						<div style='width:100%;height:1px;background:#ffffff;float:left;'><span></span></div>
						<div style='width:100%;height:1px;background:#cccccc;float:left;'><span></span></div>
						<div style='width:100%;height:2px;background:#ffffff;float:left;'><span></span></div>
						<div style='width:100%;float:left;'>
							<span id="article_title">

							<? //글 제목
									$document_title = str_replace("<", "&lt;", $document_title);
									$document_title = str_replace(">", "&gt;", $document_title);
									$document_title = str_replace("\r\n", "<br>", $document_title);       // 줄바꿈 처리
									$document_title = str_replace("\u0020", "&nbsp;", $document_title);
									$document_title = str_replace(" ", "&nbsp;", $document_title);

									echo $document_title;
							?>
							</span>
						</div>
						<div style='width:100%;height:1px;background:#ffffff;float:left;'><span></span></div>
						<div style='width:100%;height:1px;background:#cccccc;float:left;'><span></span></div>
						<div style='width:100%;height:2px;background:#ffffff;float:left;'><span></span></div>
						<div style='width:100%;float:left;'>
							<!--span id="article_content"-->
							<br>
							<textarea style='width:100%;height:10px;border:0px;background:transparent;overflow-y:hidden;' readonly id="t_doc"><? 
							
							//글 내용
							/*
							$document_content = str_replace("<", "&lt;", $document_content);
							$document_content = str_replace(">", "&gt;", $document_content);
							$document_content = str_replace("\r\n", "<br>", $document_content);       // 줄바꿈 처리
							$document_content = str_replace("\u0020", "&nbsp;", $document_content);
							$document_content = str_replace("\t", "        ", $document_content);
							$document_content = str_replace(" ", "&nbsp;", $document_content);
							*/
							echo $document_content;?></textarea>

							

							<!--/span-->
							<p align='right'>
								<? if ($_SESSION['user_idx']==$document_user_idx)
								{
								?>
									<a style='cursor:pointer;' onclick="location.href='ndd_talk_input.php?name=<? echo $board_name;?>&category_idx=<? echo $category_idx; ?>&document_idx=<? echo $document_idx; ?>&page=<? echo $page; ?>'">수정</a>
									&nbsp;|&nbsp;
									<a onclick="if(confirm('글을 정말 삭제하시겠습니까?')) ifr_temp.location.href='ndd_talk_delete.php?idx=<? echo $document_idx;?>';" style='cursor:pointer;' >삭제</a>

								<?
								}
								?>

								
								<!--a style='cursor:pointer;' onclick="location.href='board.php?name=<? echo $board_name;?>&category_idx=<? echo $category_idx; ?>&page=<? echo $page; ?>';">목록보기</a-->
							</p>
						</div>


						<div style='width:100%;height:1px;background:#ffffff;float:left;'><span></span></div>
						<div style='width:100%;height:1px;background:#cccccc;float:left;'><span></span></div>
						<div style='width:100%;height:2px;background:#ffffff;float:left;'><span></span></div>
						<div style='width:100%;float:left;'>
							<span id="article_title">
								이 이야기에 대한 답글

							</span>
						</div>
						<div style='width:100%;height:1px;background:#ffffff;float:left;'><span></span></div>
						<div style='width:100%;height:1px;background:#cccccc;float:left;'><span></span></div>
						<div style='width:100%;height:2px;background:#ffffff;float:left;'><span></span></div>

<!-- 댓글출력 시작 -->


			<?
				//댓글보기 시작

				$sql="
					SELECT comment_idx, B.nickname, content, recommend_count, accuse_count, A.reg_timestamp, A.user_idx
					
					FROM ndd_comment A, ndd_user B
					WHERE document_idx = $document_idx
					AND A.user_idx = B.user_idx
					AND A.upper_idx = 0

					ORDER BY A.reg_timestamp 
					;
				";

				$result = mysql_query($sql,$connect);

				while($row = mysql_fetch_row($result))
				{
					?>
				<div style="width:100%;" id='div_comment_<? echo $row[0]; ?>'>
		
					<div style="width:46px;float:left;">
						<div style="width:46px; height:13px;  float:left;"><span></span></div>
						
						<div style="width:32px; height:32px; background:#eeeeee; float:left;">
							<?
								$user_img = "./upload/member_photo/".$row[6];

								if(file_exists($user_img)){
							?>
								<a onclick="show_user_menu('<? echo $row[6];?>', '<? echo $row[1];?>');"  style='cursor:pointer;'>
								<img src="<? echo $user_img; ?>" width="32px" height="32px" border=0></a>
							<?
								}
								else
								{
							?>
								<? if($row[6] == $_SESSION['user_idx'])
									{
								?>
									<a href="member_info.php">등록</a>
								<?
									}
								?>
							<?
								}
							?>
							
						</div>							
						<div style="width:46px; height:13px;  float:left;"><span></span></div>
					</div>


					<div style="width:280px;float:left;">
						<div style="width:100%; height:13px;  float:left;"><span></span></div>
						<div style="width:100%; float:left;">

					<? if ($row[4] >= 5) { ?>
							<a onclick="var nsns=this.nextSibling.nextSibling; nsns.style.visibility=nsns.style.visibility=='hidden'?'visible':'hidden';" style="float:left;cursor:pointer;">신고를 많이 받은 댓글입니다.</a>
							<div style="visibility:hidden;clear:both;"><?
					} ?>



							<span id='list_id'><? //댓글 작성자
							echo $row[1];?></span> <? //echo $row[5];?><br>

							<? //댓글 내용
							$content = $row[2];
							$content = str_replace("<", "&lt;", $content);
							$content = str_replace(">", "&gt;", $content);
							$content = str_replace("\r\n", "<br>", $content);       // 줄바꿈 처리
							$content = str_replace("\u0020", "&nbsp;", $content);
							$content = str_replace("\t", "        ", $content);
							$content = str_replace(" ", "&nbsp;", $content);

							echo $content;?>
					<? if ($row[4] >= 5) { ?>
							</div>
					<? } ?>

						</div>
						<div style="width:100%; height:13px;  float:left;"><span></span></div>
					</div>

					<div style="float:left;width:100%;text-align:right;">
						<?
							if($row[6]==$_SESSION['user_idx'])
							{

						?>
							<a onclick="
							document.getElementById('div_comment_modify_<? echo $row[0]; ?>').style.display='';
							document.getElementById('div_comment_<? echo $row[0]; ?>').style.display='none';
							textareaSetLine(document.getElementById('content_<? echo $row[0]; ?>'));
							" style='cursor:pointer;' >수정</a> &nbsp;|&nbsp; 
						<a onclick="if(confirm('댓글을 정말 삭제하시겠습니까?')) ifr_temp.location.href='board_comment_delete.php?idx=<? echo $row[0];?>';" style='cursor:pointer;' >삭제</a>
						<?
							}
						?>
					</div>				
					<div style="clear:both;"></div>				
				</div>

				<?
					if($row[6]==$_SESSION['user_idx'])
					{

				?>		
					<div style="width:100%;display:none;" id='div_comment_modify_<? echo $row[0]; ?>'>
						<form action='board_comment_add.php' method='post' name='frm_comment_<? echo $row[0]; ?>'>			
							<input type='hidden' name='comment_idx' value='<? echo $row[0]; ?>'>
							<input type='hidden' name='current_location_<? echo $row[0]; ?>'>
							<script>
								document.getElementsByName('current_location_<? echo $row[0]; ?>')[0].value = location.href;
							</script>

							<div style="width:46px;float:left;">
								<div style="width:46px; height:13px;  float:left;"><span></span></div>
								
								<div style="width:32px; height:32px; background:#eeeeee; float:left;">
									<?
										$user_img = "./upload/member_photo/".$row[6];

										if(file_exists($user_img)){
									?>
										<a onclick="show_user_menu('<? echo $row[6];?>', '<? echo $row[1];?>');"  style='cursor:pointer;'>
										<img src="<? echo $user_img; ?>" width="32px" height="32px" border=0></a>
									<?
										}
										else
										{
									?>
										<? if($row[6] == $_SESSION['user_idx'])
											{
										?>
											<a href="member_info.php">등록</a>
										<?
											}
										?>
									<?
										}
									?>
									
								</div>							
								<div style="width:46px; height:13px;  float:left;"><span></span></div>
							</div>

							<div style="width:280px;float:left;">
								<div style="width:100%; height:13px;  float:left;"><span></span></div>
								<div style="width:100%; float:left;">
									<span id='list_id'><? //댓글 작성자
									echo $row[1];?></span> <? //echo $row[5];?><br>
									<textarea name='content' id='content_<? echo $row[0]; ?>' style='width:100%;height:55px;' onKeyup="textareaSetLine(this);"><? echo $row[2]; ?></textarea><br>
								</div>
								<div style="width:100%; height:13px;  float:left;"><span></span></div>
							</div>

							<div style="float:left;width:100%;text-align:right;">

								<a onclick='add_comment(<? echo $row[0]; ?>);' style='cursor:pointer;'>쓰기</a> &nbsp;|&nbsp; 
								<a onclick="document.getElementById('div_comment_modify_<? echo $row[0]; ?>').style.display='none';document.getElementById('div_comment_<? echo $row[0]; ?>').style.display='';" style='cursor:pointer;' >취소</a>

							</div>				
							<div style="clear:both;"></div>				
						</form>
					</div>			
				<?
					}

					?>



						<div style='width:100%;height:1px;background:#ffffff;float:left;'><span></span></div>
						<div style='width:100%;height:1px;background:#cccccc;float:left;'><span></span></div>
						<div style='width:100%;height:2px;background:#ffffff;float:left;'><span></span></div>
						
				<?

				}
			?>

			<div style='width:100%;height:15px;float:left;'><span></span></div>

			<? //댓글작성 ?>

			<form action='board_comment_add.php' method='post' name='frm_comment' id="frm_comment">
				<input type='hidden' name='document_idx' value='<? echo $document_idx; ?>'>
				<input type='hidden' name='board_idx' value='<? echo $board_idx; ?>'>
				<input type='hidden' name='current_location'>
				<script>
					document.getElementsByName('current_location')[0].value = location.href;
				</script>

				

				<div style="clear:both;"></div>	
				<div style="width:100%;">

					<div style="float:left;width:20px;height:15px;"></div>
					<div style="float:left;width:280px;">
						<textarea id="comment_new" style='width:100%;height:55px;'  onKeyup="textareaSetLine(this);" ></textarea><br>
					</div>
					<div style="float:left;width:100%;height:15px;"></div>
					<div style="float:left;width:95%;text-align:right;">
						<a onclick="add_comment('');" style='cursor:pointer;'><img src='./img/ndd_talk_save.png'></a>
						<!--a onclick="$('#frm_comment').hide();" style='cursor:pointer;'><img src='./img/ndd_talk_cancel.png'></a-->

					</div>
					<div style="clear:both;"></div>

					
					<SCRIPT type="text/javascript">
					 function textareaSetLine(textarea)
					 {
						if(textarea.style.pixelHeight>0)
						 {
					 
							  var currentHeight = textarea.style.pixelHeight;
							  var overHeight = textarea.scrollHeight-5;
							  if(overHeight >= currentHeight)
							   textarea.style.pixelHeight = overHeight + 5;
					 
						 }
						 else
						 {


							  if(textarea.scrollHeight > textarea.clientHeight)
							 {
							   textarea.style.height = (textarea.scrollHeight + 5)+'px';
							 }
						 }
					 }
					</SCRIPT>

				</div><br>	
			</form><br>

			<script>
			$('#frm_comment').hide();
			</script>
				
			<div style="clear:both;"></div>				



<!-- 댓글 출력 끝-->




					<div style='width:167px;height:60px;float:left;cursor:pointer' onclick="$('#frm_comment').show();">
						<img src='./img/ndd_talk_button_comment.png'> 
					</div>
					<div style='width:159px;height:60px;float:left;cursor:pointer'>
						<a href='5_1.php?show_idx=<?=$document_user_idx?>'>
							<img src='./img/ndd_talk_button_view_chart.png' border='0'/> 
						</a>
					</div>
					<!--div style='width:100%px;height:30px;float:left;'><span></span></div-->
				</div>



				<script>
				function add_comment(comment_idx)
				{
					

					var user_idx = '<? echo $user_idx;?>';
					var board_idx = '<? echo $board_idx;?>';
					var document_idx = '<? echo $document_idx;?>';

					var new_comment = $('#comment_new').attr('value');

					if(comment_idx !="")
						new_comment = $('#content_'+comment_idx).attr('value');
	

					 $.post(
						"board_comment_add.php", //서버측 스크립트 언어
						{ 	'content' : new_comment ,
							'document_idx' : document_idx,
							'board_idx' : board_idx,
							'comment_idx' : comment_idx


						}, //쿼리스트링?키1=값1&키2=값2
						function (data) {         //콜백함수 : 결과값 받기 , data로 받아옴  
							//$('#pnlDisplay').html(data);
//						$('#div_sub_comment_'+upper_idx).load('board_sub_comment.php', {'user_idx' : '<? echo $user_idx; ?>', 'upper_idx' : upper_idx});
//						$('#new_sub_comment_' + upper_idx).val('');
							talk_display_article(<? echo $document_idx; ?>);

	//						alert(data);
						}
					); 

				}



				$(document).ready(function() {
				  // Handler for .ready() called.
					textareaSetLine(document.getElementById('t_doc'));
					setPosition();
				});


				</script>

			</div>

			<? include "footer.php";?>
		</div>